package com.example.shoppingmallclient;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

@SuppressWarnings("all")
public class Register extends AppCompatActivity {

    private static final int EXISTS = -1; //已经存在
    private static final int ERROR = 0; //注册失败
    private static final int SUCCESS = 1; //注册成功

    private Button btnRegister;
    private EditText evUserNumber;
    private EditText evPassword;
    private EditText evName;

    private Handler myHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case EXISTS:
                    Toast.makeText(Register.this, "该账号已被注册", Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS:
                    Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                    break;
                case ERROR:
                    Toast.makeText(Register.this, "注册失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        findViews();
        setListeners();
    }

    private void findViews(){
        btnRegister = findViewById(R.id.btn_login);
        evUserNumber = findViewById(R.id.usernumber);
        evPassword = findViewById(R.id.password);
        evName = findViewById(R.id.name);
    }

    private void setListeners(){
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
    }

    private void register(){
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL("http://10.7.86.32:8080/RegisterServlet");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");

                    String userNumber = evUserNumber.getText().toString();
                    String password = evPassword.getText().toString();
                    String name = evName.getText().toString();
                    String msg = userNumber + "," + password + "," + name;
                    System.out.println(msg);
                    OutputStream os = connection.getOutputStream();
                    os.write(msg.getBytes());
                    os.flush(); //刷新缓存

                    //接收服务器发来的消息
                    InputStream is = connection.getInputStream();
                    StringBuffer stringBuffer = new StringBuffer();
                    int len = 0;
                    byte[] bytes = new byte[1024];
                    while ((len = is.read(bytes)) != -1){
                        stringBuffer.append(new String(bytes, 0, len));
                    }
                    Integer result = Integer.valueOf(stringBuffer.toString());
                    System.out.println(result);
                    Message message = new Message();
                    if(result == EXISTS){
                        message.what = EXISTS;
                        myHandler.sendMessage(message);
                    }else if(result == SUCCESS){
                        message.what = SUCCESS;
                        myHandler.sendMessage(message);
                    }else {
                        message.what = ERROR;
                        myHandler.sendMessage(message);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}